package com.tenement.controller.foreground.house;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.mybatis.entity.House;
import com.tenement.mybatis.entity.Photo;
import com.tenement.service.interfaces.IHouseService;
import com.tenement.service.interfaces.IPhotoService;
import com.tenement.utils.ftp_images_server.FtpConfig;
import com.tenement.utils.ftp_images_server.FtpUtil;
import com.tenement.utils.ftp_images_server.UploadUtils;

/**
 * 用户发布房源
 * 
 * @author wk
 *
 */
@Controller
public class PublishMsgController {

	private static final Log logger = LogFactory.getLog(PublishMsgController.class);

	@Autowired
	private IPhotoService iPhotoService;

	@Autowired
	private IHouseService iHouseService;

	/**
	 * 进入发布房源页面 ;需要进行登录验证，session中无用户登录信息则直接返回到登录界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/publishMsgHtml.action")
	public String toPublishMsg() {
		logger.info("【-进入发布房源页面publishHouseMsg.html-】");
		return "redirect:/view/foreground/house/publishHouseMsg.html";
	}

	/**
	 * 信息发布
	 * 
	 * @param albumId
	 * @param files
	 * @param h_rental_method
	 * @param h_area
	 * @param h_region
	 * @param h_location
	 * @param h_room
	 * @param h_hall
	 * @param h_bathroom
	 * @param h_kitchen
	 * @param h_size
	 * @param h_heading
	 * @param h_decoration
	 * @param h_rent
	 * @param h_paymethod
	 * @param h_floor
	 * @param h_floor_total
	 * @param h_caption
	 * @param h_personname
	 * @param h_telephone
	 * @return
	 */
	@RequestMapping(value = "/publishHouseMsg.action")
	public @ResponseBody String publishHouseMsg(@RequestParam("albumId") Integer albumId,
			@RequestParam("file") MultipartFile[] files, @RequestParam("h_rental_method") String h_rental_method,
			@RequestParam("h_area") String h_area, @RequestParam("h_region") String h_region,
			@RequestParam("h_location") String h_location, @RequestParam("h_room") String h_room,
			@RequestParam("h_hall") String h_hall, @RequestParam("h_bathroom") String h_bathroom,
			@RequestParam("h_kitchen") String h_kitchen, @RequestParam("h_size") String h_size,
			@RequestParam("h_heading") String h_heading, @RequestParam("h_decoration") String h_decoration,
			@RequestParam("h_rent") String h_rent, @RequestParam("h_paymethod") String h_paymethod,
			@RequestParam("h_floor") String h_floor, @RequestParam("h_floor_total") String h_floor_total,
			@RequestParam("h_caption") String h_caption, @RequestParam("h_personname") String h_personname,
			@RequestParam("h_telephone") String h_telephone) {
		logger.info("【-信息发布数据提交-】");
		House house = new House();
		String h_photoid = UUID.randomUUID().toString();
		house.sethPhotoid(h_photoid);// 1.图片id与photo表对应java,UUID生成的唯一标识
		// 基础信息
		house.sethRentalMethod(h_rental_method);// 1.出租方式，整租合租
		house.sethArea(h_area);// 2.区域
		house.sethRegion(h_region);// 3.下属区域
		house.sethLocation(h_location);// 4.具体位置
		house.sethRoom(Integer.parseInt(h_room));// 5.几室
		house.sethHall(Integer.parseInt(h_hall));// 6.几厅
		house.sethBathroom(Integer.parseInt(h_bathroom));// 7.几卫生间
		house.sethKitchen(Integer.parseInt(h_kitchen));// 8.几厨
		house.sethSize(h_size);// 9.房屋面积
		house.sethHeading(h_heading);// 10.房屋朝向
		house.sethDecoration(Integer.parseInt(h_decoration));// 11.房屋装修情况
		house.sethFloor(h_floor);// 12.第几层
		house.sethFloorTotal(Integer.parseInt(h_floor_total));// 13.共几层
		house.sethRent(h_rent);// 14.房屋租金
		house.sethPaymethod(h_paymethod);// 15.付费方式，压一付一
		house.sethCommunityName("融泽嘉园");
		house.setuId(4);
		// 详细信息
		house.sethCaption(h_caption);// 16.信息标题
		// 图片file
		savePhotos(files, albumId, h_photoid);
		// 其他信息
		house.sethPersonname(h_personname);// 17.联系人姓名
		house.sethTelephone(h_telephone);// 18.联系人手机号
		boolean b = iHouseService.publishHouseMsg(house);
		if (b)
			return state.Success;
		else
			return state.Failed;

	}

	/**
	 * 上传图片到服务器，信息发布
	 * 
	 * @param files
	 * @param albumId
	 * @param h_photoid
	 */
	public void savePhotos(MultipartFile[] files, Integer albumId, String h_photoid) {
		logger.info("【上传图片controller】");
		FtpConfig ftpConfig = new FtpConfig();
		List<Photo> photoList = new ArrayList<Photo>();
		for (MultipartFile file : files) {
			Photo photo = new Photo();
			String oldName = file.getOriginalFilename();// 获取图片原来的名字
			String picNewName = UploadUtils.generateRandonFileName(oldName);// 通过工具类产生新图片名称，防止重名
			String picSavePath = UploadUtils.generateRandomDir(picNewName);// 通过工具类把图片目录分级
			photo.setpPhotoUrl(picSavePath + "/");// 设置图片的url--》就是存储到数据库的字符串url
			photo.setpAlbumid(albumId);// 设置图片所属相册id
			photo.sethPhotoid(h_photoid);// 房屋图片id与photo表对应
			photo.setpPhotoName(picNewName);// 图片名称
			photoList.add(photo);
			try {
				FtpUtil.pictureUploadByConfig(ftpConfig, picNewName, picSavePath, file.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 上传到图片服务器的操作 // 添加到数据库
		iPhotoService.uploadImages(photoList);
	}
}
